package com.suikajy.damaiproject.util;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

import java.lang.ref.WeakReference;

/**
 * Created by suikajy on 2016/12/12.
 */

public class RefreshBar extends View {

    private int mHeight;
    private int mWidth;
    private Paint mPaint;
    private Rect[] mRects;
    private int mMoveRangeX;
    private int mCurrentOffsetX;
    private int mMoveOffset;
    private MoveTask mMoveTask;
    private boolean isScroll = false;

    public RefreshBar(Context context) {
        this(context, null);
    }

    public RefreshBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RefreshBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.WHITE);
        mRects = new Rect[4];
        setBackgroundColor(Color.parseColor("#FFCC66"));
        mMoveTask = new MoveTask(this);
        for (int i = 0; i < mRects.length; i++) {
            mRects[i] = new Rect();
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mHeight = getMeasuredHeight();
        mWidth = getMeasuredWidth();
        mMoveRangeX = mWidth >> 2;
        mMoveOffset = mMoveRangeX >> 5;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        for (int i = 0; i < mRects.length; i++) {
            mRects[i].set(i * mMoveRangeX + mCurrentOffsetX, 0, i * mMoveRangeX + (mHeight << 1) + mCurrentOffsetX, mHeight);
            canvas.drawRect(mRects[i], mPaint);
        }

        //32毫秒一帧是全动画的基准
        if (isScroll) {
            postDelayed(mMoveTask, 32);
        }
    }

    public void startRefresh() {
        if (mMoveTask != null) {
            isScroll = true;
            removeCallbacks(mMoveTask);
            postDelayed(mMoveTask, 32);
        }
    }

    public void stopRefresh() {
        isScroll = false;
        removeCallbacks(mMoveTask);
    }

    private static class MoveTask implements Runnable {

        private WeakReference<RefreshBar> reference;

        public MoveTask(RefreshBar refreshBar) {
            reference = new WeakReference<>(refreshBar);
        }

        @Override
        public void run() {
            if (reference != null) {
                RefreshBar refreshBar = reference.get();
                refreshBar.mCurrentOffsetX += refreshBar.mMoveOffset;
                if (refreshBar.mCurrentOffsetX >= refreshBar.mMoveRangeX) {
                    refreshBar.mCurrentOffsetX = 0;
                }
                refreshBar.invalidate();
            }
        }
    }
}
